import { Component, OnInit, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
import { _HttpClient, SettingsService } from '@delon/theme';
import { NzMessageService } from 'ng-zorro-antd';

/**
 * 用户安全设置
 */
@Component({
  selector: 'app-userinfo-security',
  templateUrl: './security-component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class UserSecurityComponent implements OnInit {
  userLoading = true;
  user = null;
  level = '弱';
  process = 0;
  constructor(
    public http: _HttpClient,
    private setting: SettingsService,
    public msg: NzMessageService,
    private cdr: ChangeDetectorRef,
  ) {}

  ngOnInit() {
    this.http.get(`/cf/admin/user/${this.setting.user.userId}`).subscribe(res => {
      this.userLoading = false;
      this.user = res.data;
      if (this.user.secLevel === '弱') {
        this.process = 10;
      }
      if (this.user.secLevel === '中') {
        this.process = 30;
      }
      if (this.user.secLevel === '强') {
        this.process = 50;
      }
      if (this.user.phone) {
        this.process += 25;
      }
      if (this.user.secEmail) {
        this.process += 25;
      }
      if (this.process > 80) {
        this.level = '强';
      } else if (this.process >= 50) {
        this.level = '中';
      } else {
        this.level = '弱';
      }
      this.cdr.detectChanges();
    });
  }
}
